package org.springframework.validation.rules;

import java.util.ArrayList;
import java.util.List;

public class ValidationRule {
	
	protected String path;
	protected String type;
	protected String value;
	protected String message;
	protected String errorPath;
	
	protected List<ValidationRule> rules;
	
	public ValidationRule(String path, String type, String value) {
		this.path = path;
		this.type = type;
		this.value = value;
		this.rules = new ArrayList<ValidationRule>();
	}
	
	public ValidationRule cloneWithPath(String path) {
		ValidationRule newRule = new ValidationRule(path, this.type, this.value);
		newRule.setErrorPath(this.errorPath);
		newRule.setMessage(this.message);
		newRule.setRules(this.rules);
		return newRule;
	}
	
	public boolean hasChildren() {
		return this.rules != null && !this.rules.isEmpty();
	}
	
	@Override
	public String toString() {
		String rule = "<rule ";
		if (path != null && path.length() > 0) {
			rule += "path=\"" + path + "\" ";
		}
		if (type != null && type.length() > 0) {
			rule += "type=\"" + type + "\" ";
		}
		if (value != null && value.length() > 0) {
			rule += "value=\"" + value + "\" ";
		}
		if (message != null && message.length() > 0) {
			rule += "message=\"" + message + "\" "; 
		}
		if (errorPath != null && errorPath.length() > 0) {
			rule += "errorPath=\"" + errorPath + "\" "; 
		}
		rule += "/>";
		return rule;
	}
	
	public void addRule(ValidationRule rule)
	{
		this.rules.add(rule);
	}
	
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	/**
	 * @return the rules
	 */
	public List<ValidationRule> getRules() {
		return rules;
	}

	/**
	 * @param rules the rules to set
	 */
	public void setRules(List<ValidationRule> rules) {
		this.rules = rules;
	}
	
	/**
	 * @return the message
	 */
	public synchronized String getMessage() {
		return message;
	}

	/**
	 * @param message the message to set
	 */
	public synchronized void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the errorPath
	 */
	public String getErrorPath() {
		return errorPath;
	}

	/**
	 * @param errorPath the errorPath to set
	 */
	public void setErrorPath(String errorPath) {
		this.errorPath = errorPath;
	}
	
	
}
